// pages/safetylieb/safetylieb.js
const app = getApp();
var sliderWidth = 96;
Page({
    /**
     * 页面的初始数据
     */
    /**searchbar */
    data: {
        userInfo: {},
        bususer: {},
        currentTab: 1,
        inputShowed: false,
        inputVal: "",
        tabs: ["待确认", "待整改", "已完成", "未检查"],
        array: [],
        index:11,
        isConfirm: 1,
        currentIndex: 0,
        isloading: false,
        pagesize: 10,
        datalist: [],
        list:[],
        scrollheight:300,
        scrollheightdriver:300,
        unchecknumber:0,
        useropenid: null
    },

    genDateArray:function(){
      var thisYear=new Date().getFullYear();
      var startYear=thisYear-2;
      var thisMonth=new Date().getMonth()+1;
      var result=[];
      for(var i=startYear;i<=thisYear;i++){
        var preStr=i+"";
        for (var j = 1; (i < thisYear && j <= 12)||(i ==thisYear&&j<=thisMonth);j++){
          if(j<10){
            j="0"+j;
          }
          result.push(preStr+"-"+j+"");
        }
      }
      console.log(result);
      return result;
    },

    bindPickerChange: function (e) {
        this.setData({ index: e.detail.value });
        this.refreshData();
    },

    loaddriverdata: function (status,callback) {
      if (this.data.isloading) {
        return;
      }
      this.setData({ isloading: true });
        wx.showLoading({
            title: '正在加载数据'
        })
        var that = this
        wx.request({
            method: 'GET',
            url: app.globalData.serverPath + "api/get-bs-car-safe-checks-list",
            header: {
                'Authorization': app.globalData.token
            },
            data: {
                isConfirm: status,
                page: this.data.currentIndex,
                size: this.data.pagesize
            },
            success: function (res) {
                that.setData({ datalist: that.data.datalist.concat(res.data.content) });
                that.setData({currentIndex:res.data.number});
                if (res.data.last) {//最后一页，停止所有加载
                  that.setData({ isloading: true });
                } else {
                  that.setData({ isloading: false });
                }
                if (callback) {
                    callback(res.data);
                }
            },
            complete:function(){
              wx.hideLoading();
            }
        })
    },
    /**加载待确认，待整改，已完成数据 */
    loadsafetyliebdata: function (isconfirm, isrect, time,plateNumber,callback) {
        if(this.data.isloading){
          return;
        }
        if (!plateNumber) plateNumber="";
        this.setData({isloading:true});
        wx.showLoading({
            title: '正在加载数据'
        })
        var that = this;
        wx.request({
            method: 'POST',
            url: app.globalData.serverPath + "api/bs-car-safe-checks-list",
            header: {
                'Authorization': app.globalData.token,
                'content-type':'application/x-www-form-urlencoded'
            },
            data: {
                is_confirm: isconfirm,
                is_rect: isrect,
                plateNumber: plateNumber,
                yyyy_mm: time,
                page: this.data.currentIndex,
                size: this.data.pagesize
            },
            success: function (res) {
                console.log(res)
                if (res.data.last) {//最后一页，停止所有加载
                  that.setData({ isloading: true });
                } else {
                  that.setData({ isloading: false });
                }
                that.setData({ list: that.data.list.concat(res.data.content)});
                that.setData({ currentIndex: res.data.number });
                if (callback) {
                    callback(res.data);
                }
            },
            complete:function(){
              wx.hideLoading();
            }
        })
    },
/**加载未检查数据 */
    loadnosafetyliebdata: function (time,plateNumber,callback) {
      if (this.data.isloading) {
        return;
      }
      if (!plateNumber) plateNumber = "";
      this.setData({ isloading: true });
        wx.showLoading({
            title: '正在加载数据'
        })
        var yyy = time.split("-")[0];
        var mm = time.split("-")[1];

        var that = this
        wx.request({
            method: 'GET',
            url: app.globalData.serverPath + "api/bs-cars-uncheck-list",
            header: {
                'Authorization': app.globalData.token,
            },
            data: {
                yyyy: yyy,
                mm: mm,
                plateNumber: plateNumber,
                page: this.data.currentIndex,
                size: this.data.pagesize
            },
            success: function (res) {
                console.log(res)
                that.setData({ 
                  list: that.data.list.concat(res.data.content),
                  currentIndex: res.data.number,
                  unchecknumber: res.data.totalElements
                });
                
                if (res.data.last){//最后一页，停止所有加载
                  that.setData({ isloading: true });
                }else{
                  that.setData({ isloading: false });
                }
                if (callback) {
                    callback(res.data);
                }
            },
            complete:function(){
              wx.hideLoading();
            }
        })
    },
    unConfirmclick: function () {
        this.setData({ isConfirm: 0 });
        this.refreshData()
    },

    confirmClick: function () {
        this.setData({ isConfirm: 1 });
        this.refreshData()
    },

    /**获取安全员各tab页面data */
    loadsafetytabdata:function(e){
      var tab = e.currentTarget.dataset.current;
      //初始化页面参数
      this.setData({
        currentTab: tab,
        isloading: false,
        currentIndex: 0,
        inputVal: "",
        list:[]
      });
      if(tab!=3){
        if(tab==0){//待确认
          this.loadsafetyliebdata(0, 0, this.data.array[this.data.index]);
        } else if (tab == 1) {//待整改
          this.loadsafetyliebdata(1, 0, this.data.array[this.data.index]);
        } if (tab == 2) {//已完成
          this.loadsafetyliebdata(1, 1, this.data.array[this.data.index]);
        }
      }else{
        this.loadnosafetyliebdata(this.data.array[this.data.index]);
      }
      
    },

    
    showitem: function () {
        this.setData({
            open: !this.data.open
        })
    },

    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    /**搜索按钮 */
    searchInput: function () {
        var val=this.data.inputVal;
        var tab = this.data.currentTab;
        this.setData({
          currentTab: tab,
          isloading: false,
          currentIndex: 0,
          list: []
        });
        if ( tab!= 3) {
          if (tab == 0) {//待确认
            this.loadsafetyliebdata(0, 0, this.data.array[this.data.index],val);
          } else if (tab == 1) {//待整改
            this.loadsafetyliebdata(1, 0, this.data.array[this.data.index],val);
          } if (tab == 2) {//已完成
            this.loadsafetyliebdata(1, 1, this.data.array[this.data.index],val);
          }
        } else {
          this.loadnosafetyliebdata(this.data.array[this.data.index],val);
        }
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    },


    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },


    gotocheck:function(e){
        var car=e.currentTarget.dataset.car;
        var driver = e.currentTarget.dataset.driver;
        var month = e.currentTarget.dataset.month;
        var d=new Date();
        var dmonth = d.getMonth() + 1;
        if(dmonth<10){
          dmonth="0"+dmonth;
        }
        var currentMonth = (d.getFullYear() + "-" + (dmonth));
        if(currentMonth!=month){
            wx.showModal({
                title: '错误提示',
                content: '只能发起当月车辆检查',
                showCancel:false
            })
            return;
        }
        app.globalData.checkCar=car;
        app.globalData.checkDriver=driver;
        wx.navigateTo({
            url: '../main-safety/main-safety'
        })
    },

    /**获取浮动数字 */
    getfloatnumber:function(time){
      var yyy = time.split("-")[0];
      var mm = time.split("-")[1];
      var that=this;
      //获取未检查
      wx.request({
        method: 'GET',
        url: app.globalData.serverPath + "api/bs-cars-uncheck-list",
        header: {
          'Authorization': app.globalData.token,
        },
        data: {
          yyyy: yyy,
          mm: mm,
          plateNumber: "",
          page: this.data.currentIndex,
          size: this.data.pagesize
        },
        success: function (res) {
          that.setData({
            unchecknumber: res.data.totalElements
          });
        },
        complete: function () {
        }
      })
      //获取待整改


    },
    refreshData:function(){
      //司机
      if (this.data.bususer.userType == 2 || this.data.bususer.userType == '2') {
        this.setData({
          isloading: false,
          currentIndex: 0,
          datalist: []
        });
        this.loaddriverdata(this.data.isConfirm);
      }else{//安全员
        var tab = this.data.currentTab;
        var val = this.data.inputVal;
        this.setData({
          currentTab: tab,
          isloading: false,
          currentIndex: 0,
          list: []
        });
        this.getfloatnumber(this.data.array[this.data.index]);
        if (tab != 3) {
          if (tab == 0) {//待确认
            this.loadsafetyliebdata(0, 0, this.data.array[this.data.index], val);
          } else if (tab == 1) {//待整改
            this.loadsafetyliebdata(1, 0, this.data.array[this.data.index], val);
          } if (tab == 2) {//已完成
            this.loadsafetyliebdata(1, 1, this.data.array[this.data.index], val);
          }
        } else {
          this.loadnosafetyliebdata(this.data.array[this.data.index], val);
        }
      }
      
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        scrollheight: app.globalData.windowHeight * 0.75961,
        scrollheightdriver: app.globalData.windowHeight * 0.88,
        array: this.genDateArray()
      });
      this.setData({
        index: this.data.array.length - 1
      })

      if (app.globalData.userInfo) {
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      }
      if (app.globalData.bususer) {
        this.setData({
          bususer: app.globalData.bususer
        })
      }
        
        

    },

  /**安全员滚动翻页 */
    nextpage:function(){
      console.log("bottom");
      this.setData({
        currentIndex: this.data.currentIndex+1
      });

      var tab = this.data.currentTab;
      var val = this.data.inputVal;

      if (tab==3){
        this.loadnosafetyliebdata(this.data.array[this.data.index],val);
      }else{
        if (tab == 0) {//待确认
          this.loadsafetyliebdata(0, 0, this.data.array[this.data.index], val);
        } else if (tab == 1) {//待整改
          this.loadsafetyliebdata(1, 0, this.data.array[this.data.index], val);
        } if (tab == 2) {//已完成
          this.loadsafetyliebdata(1, 1, this.data.array[this.data.index], val);
        }
      }
      
    },

  drivernextpage:function(){
    this.setData({
      currentIndex: this.data.currentIndex + 1
    });
    this.loaddriverdata(this.data.isConfirm);
  },

    gotoconfirm:function(e){
        var id = e.currentTarget.dataset.checkid
        wx.navigateTo({
            url: '../xiangqing/xiangqing?checkid='+id
        })
    },

    gotoresult:function(e){
        var id = e.currentTarget.dataset.checkid
        wx.navigateTo({
            url: '../xiangqing/xiangqing?checkid=' + id
        })
    },
    gotofinish: function (e) {
        var id = e.currentTarget.dataset.checkid
        wx.navigateTo({
            url: '../xiangqing/xiangqing?checkid=' + id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that=this;
      this.setData({
        bususer: app.globalData.bususer
      })
      if (app.globalData.openid) {
        this.setData({
          useropenid: app.globalData.openid
        })
      }
      app.validateopenid(function(){
        if (app.globalData.loginfinish) {
          that.refreshData();
        }
      })
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    kindToggle: function (e) {
        var id = e.currentTarget.id

            , list = this.data.list;
        for (var i = 0, len = list.length; i < len; ++i) {
            if (list[i].id == id) {
                list[i].open = !list[i].open
            } else {
                list[i].open = false
            }
        }
        this.setData({
            list: list
        });
    }
})